Skip to content

Merge host request headers regardless of casing - #1788

Open
VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/request-header-case
Open

VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/request-header-case

Conversation

@VIOLETee0

Copy link
Copy Markdown

Problem / Why

requestHeaders merges the SDK headers and host headers with object spread, so differently cased spellings remain separate object keys. A host returning Authorization alongside the SDK's normalized authorization sends a comma-joined pair of bearer values instead of replacing the old value. Real localhost probes against the current source on both Bun and Node receive HTTP 401 for that request.

HTTP field names are case-insensitive; the Fetch header set operation replaces existing values for the same name. The host already takes precedence when it uses the same casing.

Changes

Merge through Headers and set() instead of a plain object, and remove the unused conversion helper. Extend the existing request-header test to cover the mixed-case override while retaining its exact-body assertion and checking an unrelated SDK header survives. No test file or extra test case is added.

Verification

  • Extended regression: 0 pass / 1 fail on unmodified main (Bearer tok, Bearer host-token), then 1 pass / 0 fail.
  • Request-header suite: 4 pass / 1 fail before, then 5 pass / 0 fail. Three related header/provider test files: 36 pass / 0 fail.
  • Actual localhost HTTP probes on Bun 1.3.14 and Node 24.20.0: the mixed-case hook sends two bearer values before the fix; after it, the server receives only the host value and returns the ok stream. No production backend or model is called. The Node probe bundles the current source for execution; it is not a published SDK build.
  • SDK typecheck passes with installed native TypeScript 7.0.2. Frozen-lockfile install reports no changes; ESLint has 0 errors / 8 existing resolver/import-order warnings. Raw Prettier checks retain existing CRLF/source style differences; comparison against main finds no new formatting differences.
  • SDK build remains blocked during declaration-generator loading by the existing ts.sys.getCurrentDirectory error. Binary build and smoke were not run because SDK build is a prerequisite.

Scope / Tradeoffs

Source-audit fix, with no existing issue claimed. Two SDK files only. This preserves the existing host-overrides-default policy while applying it consistently across casing; it does not add a new authentication policy or credential store. Existing hook omission/error behavior and the BYOK exclusion remain covered by the current tests.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch. The root cause is real: plain object spread merges headers by exact key string, so authorization (from the SDK) and Authorization (from a host hook) coexist as separate keys, and when the merged object is eventually normalized into a Headers instance (or sent raw), you get a comma-joined pair of bearer tokens instead of a clean override. Using new Headers(init?.headers) plus .set() is the correct fix since Headers is case-insensitive by spec, and it naturally preserves the "host wins" precedence you want.

The diff is small and the removal of plainHeaders is reasonable since it's unused once you merge through Headers directly.

Test changes look right: extending the existing hook test to return Authorization: 'Bearer host-token' and asserting the merged result replaces rather than concatenates is exactly the regression coverage this needs. Checking that user-agent survives is a nice touch to confirm the merge doesn't clobber unrelated SDK headers.

One thing to double check before porting: new Headers(init?.headers) will throw if init.headers is some exotic non-standard shape the SDK permits (e.g. a plain object with non-string values) — worth confirming init?.headers is always a valid HeadersInit at this call site, since the old plainHeaders helper defensively handled arrays/instances/objects. If that invariant already holds elsewhere in the codebase this is a non-issue.

Overall: correct, minimal, well-tested, in-scope (sdk/ only). Worth porting as-is.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants